package com.dianping.cat.consumer.top.model.transform;

import com.dianping.cat.consumer.top.model.entity.Domain;
import com.dianping.cat.consumer.top.model.entity.Error;
import com.dianping.cat.consumer.top.model.entity.Machine;
import com.dianping.cat.consumer.top.model.entity.Segment;
import com.dianping.cat.consumer.top.model.entity.TopReport;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.xml.sax.Attributes;

























public class DefaultSaxMaker
  extends Object
  implements IMaker<Attributes>
{
  public Domain buildDomain(Attributes attributes) {
/*  43 */     String name = attributes.getValue("name");
/*  44 */     return new Domain(name);
  }




  public Error buildError(Attributes attributes) {
/*  51 */     String id = attributes.getValue("id");
/*  52 */     String count = attributes.getValue("count");
/*  53 */     Error error = new Error(id);

/*  55 */     if (count != null) {
/*  56 */       error.setCount(((Integer)convert(Integer.class, count, Integer.valueOf(0))).intValue());
    }

/*  59 */     return error;
  }


  public Machine buildMachine(Attributes attributes) {
/*  64 */     String id = attributes.getValue("id");
/*  65 */     String count = attributes.getValue("count");
/*  66 */     Machine machine = new Machine(id);

/*  68 */     if (count != null) {
/*  69 */       machine.setCount(((Integer)convert(Integer.class, count, Integer.valueOf(0))).intValue());
    }

/*  72 */     return machine;
  }


  public Segment buildSegment(Attributes attributes) {
/*  77 */     String id = attributes.getValue("id");
    String error = attributes.getValue("error");
/*  79 */     String url = attributes.getValue("url");
/*  80 */     String urlDuration = attributes.getValue("url-duration");
/*  81 */     String service = attributes.getValue("service");
/*  82 */     String serviceDuration = attributes.getValue("service-duration");
/*  83 */     String sql = attributes.getValue("sql");
/*  84 */     String sqlDuration = attributes.getValue("sql-duration");
/*  85 */     String call = attributes.getValue("call");
/*  86 */     String callDuration = attributes.getValue("call-duration");
/*  87 */     String cache = attributes.getValue("cache");
/*  88 */     String cacheDuration = attributes.getValue("cache-duration");
/*  89 */     String urlError = attributes.getValue("url-error");
/*  90 */     String urlSum = attributes.getValue("url-sum");
/*  91 */     String serviceError = attributes.getValue("service-error");
/*  92 */     String serviceSum = attributes.getValue("service-sum");
/*  93 */     String sqlError = attributes.getValue("sql-error");
/*  94 */     String sqlSum = attributes.getValue("sql-sum");
/*  95 */     String callError = attributes.getValue("call-error");
/*  96 */     String callSum = attributes.getValue("call-sum");
/*  97 */     String cacheError = attributes.getValue("cache-error");
/*  98 */     String cacheSum = attributes.getValue("cache-sum");
/*  99 */     Segment segment = new Segment((id == null) ? null : (Integer)convert(Integer.class, id, null));

/* 101 */     if (error != null) {
/* 102 */       segment.setError(((Long)convert(Long.class, error, Long.valueOf(0L))).longValue());
    }

/* 105 */     if (url != null) {
/* 106 */       segment.setUrl(((Long)convert(Long.class, url, Long.valueOf(0L))).longValue());
    }

/* 109 */     if (urlDuration != null) {
/* 110 */       segment.setUrlDuration(toNumber(urlDuration, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 113 */     if (service != null) {
/* 114 */       segment.setService(((Long)convert(Long.class, service, Long.valueOf(0L))).longValue());
    }

/* 117 */     if (serviceDuration != null) {
/* 118 */       segment.setServiceDuration(toNumber(serviceDuration, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 121 */     if (sql != null) {
/* 122 */       segment.setSql(((Long)convert(Long.class, sql, Long.valueOf(0L))).longValue());
    }

/* 125 */     if (sqlDuration != null) {
/* 126 */       segment.setSqlDuration(toNumber(sqlDuration, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 129 */     if (call != null) {
/* 130 */       segment.setCall(((Long)convert(Long.class, call, Long.valueOf(0L))).longValue());
    }

/* 133 */     if (callDuration != null) {
/* 134 */       segment.setCallDuration(toNumber(callDuration, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 137 */     if (cache != null) {
/* 138 */       segment.setCache(((Long)convert(Long.class, cache, Long.valueOf(0L))).longValue());
    }

/* 141 */     if (cacheDuration != null) {
/* 142 */       segment.setCacheDuration(toNumber(cacheDuration, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 145 */     if (urlError != null) {
/* 146 */       segment.setUrlError(((Long)convert(Long.class, urlError, Long.valueOf(0L))).longValue());
    }

/* 149 */     if (urlSum != null) {
/* 150 */       segment.setUrlSum(toNumber(urlSum, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 153 */     if (serviceError != null) {
/* 154 */       segment.setServiceError(((Long)convert(Long.class, serviceError, Long.valueOf(0L))).longValue());
    }

/* 157 */     if (serviceSum != null) {
/* 158 */       segment.setServiceSum(toNumber(serviceSum, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 161 */     if (sqlError != null) {
/* 162 */       segment.setSqlError(((Long)convert(Long.class, sqlError, Long.valueOf(0L))).longValue());
    }

/* 165 */     if (sqlSum != null) {
/* 166 */       segment.setSqlSum(toNumber(sqlSum, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 169 */     if (callError != null) {
/* 170 */       segment.setCallError(((Long)convert(Long.class, callError, Long.valueOf(0L))).longValue());
    }

/* 173 */     if (callSum != null) {
/* 174 */       segment.setCallSum(toNumber(callSum, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 177 */     if (cacheError != null) {
/* 178 */       segment.setCacheError(((Long)convert(Long.class, cacheError, Long.valueOf(0L))).longValue());
    }

/* 181 */     if (cacheSum != null) {
/* 182 */       segment.setCacheSum(toNumber(cacheSum, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 185 */     return segment;
  }


  public TopReport buildTopReport(Attributes attributes) {
/* 190 */     String domain = attributes.getValue("domain");
/* 191 */     String startTime = attributes.getValue("startTime");
/* 192 */     String endTime = attributes.getValue("endTime");
/* 193 */     TopReport topReport = new TopReport(domain);

/* 195 */     if (startTime != null) {
/* 196 */       topReport.setStartTime(toDate(startTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/* 199 */     if (endTime != null) {
/* 200 */       topReport.setEndTime(toDate(endTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/* 203 */     return topReport;
  }


  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 208 */     if (value == null || value.length() == 0) {
/* 209 */       return defaultValue;
    }

/* 212 */     if (type == Boolean.class || type == boolean.class)
/* 213 */       return (T)Boolean.valueOf(value); 
/* 214 */     if (type == Integer.class || type == int.class)
/* 215 */       return (T)Integer.valueOf(value); 
/* 216 */     if (type == Long.class || type == long.class)
/* 217 */       return (T)Long.valueOf(value); 
/* 218 */     if (type == Short.class || type == short.class)
/* 219 */       return (T)Short.valueOf(value); 
/* 220 */     if (type == Float.class || type == float.class)
/* 221 */       return (T)Float.valueOf(value); 
/* 222 */     if (type == Double.class || type == double.class)
/* 223 */       return (T)Double.valueOf(value); 
/* 224 */     if (type == Byte.class || type == byte.class)
/* 225 */       return (T)Byte.valueOf(value); 
/* 226 */     if (type == Character.class || type == char.class) {
/* 227 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 229 */     return (T)value;
  }


  protected Date toDate(String str, String format, Date defaultValue) {
/* 234 */     if (str == null || str.length() == 0) {
/* 235 */       return defaultValue;
    }

    try {
/* 239 */       return (new SimpleDateFormat(format)).parse(str);
/* 240 */     } catch (ParseException e) {
/* 241 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format, Number defaultValue) {
/* 246 */     if (str == null || str.length() == 0) {
/* 247 */       return defaultValue;
    }

    try {
/* 251 */       return (new DecimalFormat(format)).parse(str);
/* 252 */     } catch (ParseException e) {
/* 253 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/top/model/transform/DefaultSaxMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */